<template>
  <div>

    <el-form v-model="formSearch" :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="formSearch.content" placeholder="请输入搜索内容"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-row>
      <el-button type="primary" icon="el-icon-circle-plus-outline" @click="showAddProduct" plain>新增</el-button>
      <el-button type="danger" icon="el-icon-delete" @click="delBatch" plain>批量删除</el-button>
      <el-switch
          style="margin-left: 10px"
          v-model="statusValue"
          active-color="#13ce66"
          inactive-color="#ff4949"
          active-text="仅查看在售商品">
      </el-switch>

    </el-row>

    <el-table :data="productList"
              v-loading="loading"
              style="width: 100%; margin-top: 20px"
              border
              @selection-change="handleSelectionChange"
              max-height="600"
    >
      <el-table-column type="selection" align="center" width="55"></el-table-column>
      <el-table-column prop="id"
                       fixed="left"
                       label="ID"
                       sortable
                       width="100">
      </el-table-column>
      <el-table-column prop="categoryId"
                       label="分类ID"
                       width="100">
      </el-table-column>
      <el-table-column prop="name"
                       label="名称"
                       width="180">
      </el-table-column>
      <el-table-column prop="subtitle"
                       label="副标题"
                       width="240">
      </el-table-column>
      <el-table-column prop="mainImage"
                       label="主图"
                       width="100"
                       align="center">
        <template slot-scope="scope">
          <el-popover
              placement="right"
              :title="scope.row.name"
              trigger="hover">
            <img :src="scope.row.mainImage" style="max-height: 200px;"/>
            <img slot="reference" :src="scope.row.mainImage" style="max-height: 50px;max-width: 150px">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="subImages"
                       label="副图"
                       width="200"
                       show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="detail"
                       label="详情"
                       width="200"
                       show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="price"
                       label="价格"
                       width="150">
      </el-table-column>
      <el-table-column prop="stock"
                       label="库存"
                       width="150">
      </el-table-column>
      <el-table-column prop="status"
                       label="状态"
                       width="120">
        <template slot-scope="scope">

          <el-dropdown  @command="updateStatus">
            <el-button style="border: none;outline: none;box-shadow:none;background: none">
              <el-tag v-if="scope.row.status === 1" type="success">在售</el-tag>
              <el-tag v-if="scope.row.status === 2" type="info">下架</el-tag>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item :command="handleUpdateStatus(scope.row.id, 1)">在售</el-dropdown-item>
              <el-dropdown-item :command="handleUpdateStatus(scope.row.id, 2)">下架</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>

        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="120">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" @click="showEditProduct(scope.row)" circle></el-button>
          <el-button type="danger" icon="el-icon-delete" @click="handleDelete(scope.row)" circle></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        style="margin-top: 20px"
        background
        :page-sizes="[5, 10, 15, 20]"
        layout="sizes, prev, pager, next"
        :total="total"
        :current-page="currentPage"
        :page-size="pageSize"
    >
    </el-pagination>

    <!--新增商品-->
    <el-dialog title="新增商品" :visible.sync="addDialogFormVisible" width="600px">
      <el-form :model="addForm" :rules="rules">
        <el-form-item label="分类ID" :label-width="formLabelWidth" prop="username">
          <el-input v-model="addForm.categoryId"  auto-complete="off"
                    style="width: 330px"></el-input>
        </el-form-item>
        <el-form-item label="商品名称" :label-width="formLabelWidth">
          <el-input v-model="addForm.name" auto-complete="off" style="width: 330px"></el-input>
        </el-form-item>
        <el-form-item label="副标题" :label-width="formLabelWidth">
          <el-input v-model="addForm.subtitle" auto-complete="off" style="width: 330px"></el-input>
        </el-form-item>
        <el-form-item label="主图" :label-width="formLabelWidth">
          <el-input v-model="addForm.mainImage" placeholder="url相对地址" auto-complete="off"
                    style="width: 330px"></el-input>
        </el-form-item>
        <el-form-item label="副图" :label-width="formLabelWidth">
          <el-input v-model="addForm.subImages" placeholder="json格式,扩展用" auto-complete="off"
                    style="width: 330px"></el-input>
        </el-form-item>
        <el-form-item label="商品详情" :label-width="formLabelWidth">
          <el-input v-model="addForm.detail" auto-complete="off" style="width: 330px"></el-input>
        </el-form-item>
        <el-form-item label="价格" :label-width="formLabelWidth">
          <el-input v-model="addForm.price" placeholder="单位-元保留两位小数" auto-complete="off"
                    style="width: 330px"></el-input>
        </el-form-item>
        <el-form-item label="库存" :label-width="formLabelWidth">
          <el-input v-model="addForm.stock" auto-complete="off" style="width: 330px"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-select v-model="addForm.status" placeholder="1-在售 2-下架 ">
            <el-option label="在售" value="1"></el-option>
            <el-option label="下架" value="2"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAddSure">确 定</el-button>
      </div>
    </el-dialog>

    <!--编辑商品-->
    <el-dialog title="编辑商品" :visible.sync="editDialogFormVisible" width="600px" class="demo-ruleForm">
      <el-form :model="editForm" :rules="rules">
        <el-form-item label="分类ID" :label-width="formLabelWidth" prop="username">
          <el-input v-model="editForm.categoryId"  auto-complete="off"
                    style="width: 330px"></el-input>
        </el-form-item>
        <el-form-item label="商品名称" :label-width="formLabelWidth">
          <el-input v-model="editForm.name" auto-complete="off" style="width: 330px"></el-input>
        </el-form-item>
        <el-form-item label="副标题" :label-width="formLabelWidth" >
          <el-input v-model="editForm.subtitle" auto-complete="off" style="width: 330px"></el-input>
        </el-form-item>
        <el-form-item label="主图" :label-width="formLabelWidth">
          <el-input v-model="editForm.mainImage" placeholder="url相对地址" auto-complete="off"
                    style="width: 330px"></el-input>
        </el-form-item>
        <el-form-item label="副图" :label-width="formLabelWidth">
          <el-input v-model="editForm.subImages" placeholder="json格式,扩展用" auto-complete="off"
                    style="width: 330px"></el-input>
        </el-form-item>
        <el-form-item label="商品详情" :label-width="formLabelWidth">
          <el-input v-model="editForm.detail" auto-complete="off" style="width: 330px"></el-input>
        </el-form-item>
        <el-form-item label="价格" :label-width="formLabelWidth">
          <el-input v-model="editForm.price" placeholder="单位-元保留两位小数" auto-complete="off"
                    style="width: 330px"></el-input>
        </el-form-item>
        <el-form-item label="库存" :label-width="formLabelWidth">
          <el-input v-model="editForm.stock" auto-complete="off" style="width: 330px"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-select v-model="editForm.status" placeholder="1-在售 2-下架 ">
            <el-option label="在售" value="1"></el-option>
            <el-option label="下架" value="2"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleEditSure">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'admin-product',

  data() {
    return {
      productList: [],
      formSearch: {}, //搜索表单
      multipleSelection: [],
      total: 0,
      pageSize: 5,
      currentPage: 0,
      statusId: '',
      statusValue: false,
      addDialogFormVisible: false,
      editDialogFormVisible: false,
      addForm: {},
      editForm: {},
      formLabelWidth: '120px',
      rules: {
        categoryId: [
          {required: true, message: '请输入分类ID', trigger: 'blur'},
          {min: 1, max: 20, message: '默认为100012', trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请输入商品名称', trigger: 'blur'},
          {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
        ],
      },
      loading: true,
    }
  },

  mounted() {
    this.getProductList()
  },

  watch: {
    'statusValue': 'setStatus'
  },

  methods: {
    getProductList() {
      this.loading = true
      this.request.get('/product/list', {
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          content: this.formSearch.content,
          status: this.statusId
        }
      }).then((res) => {
        this.productList = res.list
        this.total = Number(res.total)
        this.loading = false
      }).catch(e => {
        this.$message.error(e)
      })
    },
    //搜索
    onSubmit() {
      this.getProductList()
    },
    //修改订单状态
    handleUpdateStatus(id, status) {
      return {
        id, status
      }
    },
    //修改订单状态
    updateStatus(command) {
      let {id, status} = command
      this.request.put('/product/status/' + id + '/' + status).then(() => {
        this.$message.success('更新成功')
        this.getProductList()
      }).catch(e => {
        this.$message.error(e)
      })
    },
    //更改是否查看在售商品状态
    setStatus() {
      this.statusValue ? this.statusId = 1 : this.statusId = ''
      this.getProductList()
    },
    //显示添加商品弹框
    showAddProduct() {
      this.addDialogFormVisible = true
      this.addForm = {}
      //新增的分类默认值
      // this.addForm.categoryId = 100012
    },
    //确认添加商品
    handleAddSure() {
      this.request.post('/product/add', this.addForm).then(() => {
        this.$message.success('添加成功')
        this.addDialogFormVisible = false
        this.getProductList()
      }).catch(e => {
        this.$message.error(e)
      })
    },
    //批量删除
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    delBatch() {
      this.$confirm("是否确认批量删除", "提示", {
        type: 'error'
      }).then(() => {
        let ids = this.multipleSelection.map(v => v.id);
        if (ids.length > 0) {
          this.request.delete('/product/' + ids).then(() => {
            this.$message.success('批量删除成功')
            location.reload()
          }).catch(e => {
            this.$message.error(e)
            location.reload()
          })
        } else {
          this.$message.info('当前未选中商品')
        }

      }).catch(() => {
        this.$message.info('取消删除')
      })
    },
    getProductOne(row) {
      this.request.get('/product/' + row.id).then((res) => {
        this.editForm = res
        this.editForm.status = res.status + ''
      }).catch(() => {
        this.$message.error('查询失败，请重试')
      })
    },
    //编辑商品弹框
    showEditProduct(row) {
      this.editDialogFormVisible = true
      this.editForm = {}
      this.getProductOne(row)
    },
    //确认编辑商品
    handleEditSure() {
      this.request.put('/product/update', this.editForm).then(() => {
        this.$message.success("修改成功")
        this.editDialogFormVisible = false
        this.getProductList()
      }).catch(e => {
        this.$message.error(e)
      })
    },
    //删除商品
    handleDelete(row) {
      this.$confirm("是否删除该商品", "提示", {
        type: 'error'
      }).then(() => {
        this.request.delete('/product/' + row.id).then(() => {
          this.$message.success("删除成功")
          this.getProductList()
        }).catch(e => {
          this.$message.error(e)
        })
      }).catch(() => {
        this.$message.info('取消删除')
      })
    },
    //分页功能
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.getProductList()
    },
    //翻页功能
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage
      this.getProductList()
    },
  }
}
</script>

<style lang="scss" scoped></style>
